var $ = require('../../utils/Ajax.js');
const QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
const wxMap = new QQMapWX({
  key: 'YQ6BZ-XJXKI-VSIGO-5ILBX-SLBK5-OVFHK'
});

// 获取位置信息函数
function GetLocation(that) {
  wx.getLocation({
    success: function (res) {
      wxMap.reverseGeocoder({
        location: {
          latitude: res.latitude,
          longitude: res.longitude
        },
        success: function(res){
          console.log(res)
          that.setData({
            CurrentLocation: res.result.address_component.city.substring(0, 2)
          })
        }
      })
    }
  })
}

Page({
  data: {
    imgUrls: [
      {
        link: '/pages/Home/Home',
        url: 'http://140.143.93.214/WX/activity.png'
      }, {
        link: '/pages/Home/Home',
        url: 'http://140.143.93.214/WX/banner_1.png'
      }, {
        link: '/pages/Home/Home',
        url: 'http://140.143.93.214/WX/experience.png'
      }, {
        link: '/pages/Home/Home',
        url: 'http://140.143.93.214/WX/banner_1.png'
      }
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    // 位置
    CurrentLocation: "西安",
    // 热门体验产品
    ExperDetails: null,
    // 收藏
    Collection: "../../images/sc_nor.png",
    // 已收藏
    IsCollection: "../../images/sc_sel.png",
    // 图片地址
    PictureUrl: getApp().PictureUrl
  },
  
  onLoad:function(){
    var that = this;
    GetLocation(that);
  },

  onShow: function () {
    var that = this;
    $.Ajax("api/x_index", "GET", {}, function (res) {
      if(res.s){
        that.setData({
          imgUrls: res.d.banner,
          ExperDetails: res.d.hot_experience
        })
        wx.setStorageSync('HotWord', JSON.stringify(res.d.seach));
      }else{
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 2000,
          mask: true
        })
      }
    });
  },

  // 监听用户下拉刷新
  onPullDownRefresh: function () {
    var that = this;
    $.Ajax("api/x_index", "GET", {}, function (res) {
      that.setData({
        imgUrls: res.d.banner,
        ExperDetails: res.d.hot_experience
      })
      wx.stopPullDownRefresh({
        success: function (res) {
          wx.showToast({
            title: '刷新成功',
            icon: 'succes',
            duration: 2000,
            mask: true
          })
        }
      })
    });
  },

  // 点击获取位置
  ClickGetLocation: function () {
    var that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting["scope.userLocation"]){
          wx.showModal({
            content: '点击确认，获取位置信息！',
            confirmText: "确认",
            cancelText: "取消",
            success: function (res) {
              //点击“确认”时打开设置页面
              if (res.confirm) {
                wx.openSetting({
                  success(res) {
                    if (res.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功！',
                        icon: 'success',
                        duration: 3000
                      })

                      //再次授权，获取位置信息
                      GetLocation(that);
                    }else{
                      wx.showToast({
                        title: '授权失败！',
                        icon: 'fail',
                        duration: 3000
                      })
                    }
                  }
                })
              } else {
                wx.showToast({
                  title: '已取消，授权失败！',
                  icon: 'fail',
                  duration: 3000
                })
              }
            }
          });
        }
      }
    })
  },

  // 点击轮播图跳转
  SwiperUrl: function(e){
    // 活动类型 0不跳转 - 1体验 - 2活动
    var Kinds = e.currentTarget.dataset.kinds;
    // 商品ID
    var Id = e.currentTarget.dataset.id;
    if (Kinds == 1){
      wx.navigateTo({
        url: '../ExperDetails/ExperDetails?Id=' + Id,
      })
    } else if (Kinds == 2){
      wx.navigateTo({
        url: '../ActiviDetails/ActiviDetails?Id=' + Id,
      })
    }
  },

  // 点击收藏
  Collection: function (e){
    var that = this;
    var Token = wx.getStorageSync('Token');
    if (Token == '' || Token == null){
      wx.switchTab({
        url: '../My/My',
      })
    }else{
      var Id = e.currentTarget.dataset.tid
      $.Ajax("api/x_add_coll", "POST", {
        id: Id,
        t: 1
      }, function (res){
        wx.showToast({
          title: res.m,
          icon: 'success',
          duration: 1000
        });
        if (res.s) {
          var CollectionIndex = e.currentTarget.dataset.homeindex;
          var is_collection = `ExperDetails[${CollectionIndex}].is_collection`;
          if (that.data.ExperDetails[CollectionIndex].is_collection == 1){
            that.setData({
              [is_collection]: 0
            })
          }else{
            that.setData({
              [is_collection]: 1
            })
          }
        }
      });
    }
  },

  // 点击体验，进入体验详情
  ExperDetails: function(e){
    var Id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../ExperDetails/ExperDetails?Id=' + Id,
    })
  },

  // 点击搜索
  ClickSearch: function(){
    wx.navigateTo({
      url: '../ClickSearch/ClickSearch',
    })
  },

  // 点击查看更多
  More: function(){
    wx.navigateTo({
      url: '../Experience/Experience',
    })
  },

  // 转发
  onShareAppMessage: function () {
    return {
      title: '弗洛觅',
      path: '/pages/Home/Home',
      success: function (res) {
        wx.showToast({
          title: "分享成功",
          icon: 'success',
          duration: 2000
        })
      },
      fail: function (res) {
        wx.showToast({
          title: "分享失败",
          icon: 'none',
          image: '../../images/err.png',
          duration: 2000
        })
      }
    }
  }
})
